package cn.cvs.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatsiUtil {

    private static SqlSessionFactory fac=null;
    private static SqlSession session=null;

    static {
        try {
            InputStream is = Resources.getResourceAsStream(Constants.mybatis_config);
//            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            fac= new SqlSessionFactoryBuilder().build(is);

        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }

    public static SqlSession createSession(){
        if (null == session){
            session = fac.openSession(false);//关闭自动提交事务
        }
        return session;
    }

    public static void closeSession(){
        if (null !=session) session.close();
    }


}
